/**
 * 自动获取焦点指令。
 */

function isInputElement(el: HTMLElement) {
  return String(el.tagName).toUpperCase() === 'INPUT';
}

function __onFocus(el, binding) {
  let inputEl = el;
  if (!isInputElement(el)) {
    inputEl = el.querySelector('input');
  }
  // el
  // 聚焦元素
  let timer;
  if (inputEl && binding.value) {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      inputEl.focus();
    }, 100);
  }
}

export default {
  mounted(el, binding) {
    __onFocus(el, binding);
  },
  updated(el, binding, preValue) {
    if (binding.arg === 'modify') {
      __onFocus(el, binding);
    }
  }
};
